﻿using HXH.Serialization.Abstractions;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace HXH.Redis
{
    /// <summary>
    /// redis客户端
    /// </summary>
     public  class RedisClinet : IRedisClient
    {

        private readonly IMessageSerializer _messageSerializer;
        private readonly IDatabase _db;

        public RedisClinet(IMessageSerializer messageSerializer,IDatabase db)
        {
            this._messageSerializer = messageSerializer;
            this._db = db;
        }
        public bool Set<T>(string key, T message, DateTime? expireTime = null)
        {
            if (expireTime != null)
            {
                return _db.StringSet(key, _messageSerializer.SerializerBytes<T>(message), (expireTime - DateTime.Now));
            }
            else
            {
                return _db.StringSet(key, _messageSerializer.SerializerBytes<T>(message));
            }
        }
        public async Task<bool> SetAsync<T>(string key, T message, DateTime? expireTime = null)
        {
            if (expireTime != null)
            {
                return await _db.StringSetAsync(key, _messageSerializer.SerializerBytes<T>(message), (expireTime - DateTime.Now));
            }
            else
            {
                return await _db.StringSetAsync(key, _messageSerializer.SerializerBytes<T>(message));
            }
        }
        public byte[] Get(string key)
        {
            var result=_db.StringGet(key);
            return Encoding.UTF8.GetBytes(result);
        }

        public T Get<T>(string key)
        {
            var result =  _db.StringGet(key);
            return _messageSerializer.Deserialize<T>(Encoding.UTF8.GetBytes(result));
        }
        public async Task<T> GetAsync<T>(string key)
        {
            var result = await _db.StringGetAsync(key);
            return _messageSerializer.Deserialize<T>(Encoding.UTF8.GetBytes(result));
        }
        public async Task<byte[]> GetAsync(string key)
        {
            var result =await _db.StringGetAsync(key);
            return Encoding.UTF8.GetBytes(result);
        }

       

        public string GetString(string key)
        {
            return _db.StringGet(key);
        }

        public async Task<string> GetStringAsync(string key)
        {
            return await _db.StringGetAsync(key);
        }

        public bool Remove(string key)
        {
            return _db.KeyDelete(key);
        }

        public async Task<bool> RemoveAsync(string key)
        {
           return await _db.KeyDeleteAsync(key);
        }

        public bool Set(string key, byte[] value, DateTime? expireTime = null)
        {
            if (expireTime != null)
            {
                return _db.StringSet(key, value,(expireTime-DateTime.Now));
            }
            else
            {
                return _db.StringSet(key, value);
            }
        }

       

        public async Task<bool> SetAsync(string key, byte[] value, DateTime? expireTime = null)
        {
            if (expireTime != null)
            {
               return await _db.StringSetAsync(key, value, (expireTime - DateTime.Now));
            }
            else
            {
               return await _db.StringSetAsync(key, value);
            }
        }

      

        public bool SetString(string key, string value, DateTime? expireTime = null)
        {
            if (expireTime != null)
            {
                return  _db.StringSet(key, value, (expireTime - DateTime.Now));
            }
            else
            {
                return  _db.StringSet(key, value);
            }
        }

        public async Task<bool> SetStringAsync(string key, string value, DateTime? expireTime = null)
        {
            if (expireTime != null)
            {
                return await _db.StringSetAsync(key, value, (expireTime - DateTime.Now));
            }
            else
            {
                return await _db.StringSetAsync(key, value);
            }
        }

    }
}
